/**
 * Author : Fengyuan(Franklin) Zhang
 * Date : 2018/12/28
 * Description : Data control
 */

var fs = require('fs');
var ControlBase = require('./controlBase');
var DataModel = require('../models/data');
var Setting = require('../setting');
var FileOpera = require('../utils/fileOpera');

var DataCtrl = function() {};
DataCtrl.__proto__ = ControlBase;
DataCtrl.model = DataModel;

module.exports = DataCtrl;

//! add data
DataCtrl.add = function (item, callback) {
    var dataItem = new DataModel(item);

    //! find extension
    var ext = item.datafile;
    ext = ext.substr(ext.lastIndexOf('.') + 1);

    //! make up full path
    var fullpath = Setting.filePath + dataItem._id + '.' + ext;

    //! get MD5
    //! TODO performance optimization
    FileOpera.getMD5(item.datafile, function (err, md5) {
        if(err){
            return callback(err);
        }

        dataItem.d_fullpath = fullpath;
        dataItem.d_md5 = md5;

        DataModel.save(dataItem, function(err, result){
            if(err){
                return callback(err);
            }
            fs.rename(item.datafile, fullpath, function(err, data) {
                if(err){
                    return callback(err);
                }
                return callback(null, result);
            });
        });
    });
}

//! get all data
DataCtrl.getAll = function (callback) {
    DataModel.getByWhere({}, this.returnFunction(callback, 'Error in getting all data items!'));
}

//! get data item in DB by OID
DataCtrl.getByOID = function (oid, callback) {
    DataModel.getByOID(oid, this.returnFunction(callback, 'Error in getting data by OID!'));
}

//! get data item by md5
DataCtrl.getByMD5 = function (md5, callback) {
    DataModel.getByWhere({d_md5 : md5}, this.returnFunction(callback, 'Error in getting data by MD5!'));
}

//! mark data as requested by item object
DataCtrl.markDataAsRequested = function (item, callback) {
    item.d_requested = true;
    DataModel.update(item, this.returnFunction(callback, 'Error in marking data as requested!'));
}

//! refresh data
DataCtrl.refresh = function (item, callback) {
    item.d_datetime = (new Date()).toISOString();
    DataModel.update(item, this.returnFunction(callback, 'Error in refresh data!'));
}

//! remove data as requested by OID
DataCtrl.removeDbItem = function (oid, callback) {
    DataModel.delete(oid, this.returnFunction(callback, 'Error in removing data item in database!'));
}

//! remove out-dated data
DataCtrl.removeOutDateData = function (callback) {
    DataModel.getByWhere({}, function (err, dataitems) {
        if(err){
            return callback(err);
        }
        var date_out = new Date();
        date_out.setDate(date_out.getDate() - 7);
        for(var i = 0; i < dataitems.length; i++){
            var data_date = new Date(dataitems[i].d_datetime);
            if(data_date < date_out){
                global.TotalSize -= dataitems[i].d_size;
                FileOpera.rmdir(dataitems[i].d_fullpath);
                DataModel.delete(dataitems[i]._id, this.returnFunction(callback, 'Error in removing data item in database!'));
            }
        }
    }.bind(this));
}